#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import RPi.GPIO as GPIO
import time
from robot_pkg.msg import Person1

class Sensor(object):

	def __init__(self):
		self.Water=0
		self.IN1 = 5  # 接DO
		# 初始化树莓派引脚，设置树莓派的引脚为输入状态
		GPIO.setwarnings(False)
		GPIO.setmode(GPIO.BCM)
		GPIO.setup(self.IN1, GPIO.IN)

		

	def get_data(self):
		if(GPIO.input(self.IN1)):
			self.Water=0
		else:
			self.Water=1  
		
		
	def sensor_publisher(self):
		# ROS节点初始化
		rospy.init_node('sensor_publisher', anonymous=True)
		person_info_pub = rospy.Publisher('/sensor_publisher', Person1, queue_size=100)
		#设置循环的频率
		rate = rospy.Rate(10) 
		while not rospy.is_shutdown():	
			self.get_data()
			# person_msg::Person类型的消息
			person_msg = Person1()
			person_msg.Water=self.Water
			person_info_pub.publish(person_msg)
			rate.sleep()

if __name__ == '__main__':
    try:
        p = Sensor()
        p.sensor_publisher()
    except rospy.ROSInterruptException:
        pass
